package lovo01.lesson05;

import javax.swing.*;
import java.util.Scanner;

public class Money {
    public static void main(String[] args) {
        while (true) {
            System.out.println("请输入货币数字: ");
            Scanner sc = new Scanner(System.in);
            String input = sc.next();
            String pattern = "\\d{1,}[.]\\d{1,}";

            if (input.matches(pattern)) {
                input.indexOf(".");
                String str = input.substring(0, input.indexOf("."));
                String str2 = input.substring(input.indexOf("."));
                StringBuffer stringBuffer = new StringBuffer(str);
                stringBuffer.reverse();
                str = stringBuffer.toString();
                char[] chars = str.toCharArray();
                for (int i = 0; i < chars.length; i++) {
                    if (i % 3 == 0 && i != 0) {
                        stringBuffer.insert(i, ",");
                    }
                }
                stringBuffer.reverse().append(str2);
                System.out.println("输出为: " + stringBuffer);
                break;
            } else {
                System.out.println("输入有误");
            }
        }
    }
}
